//
//  XLMainTabBarController.swift
//  WeiboTT
//
//  Created by 王嘉涛 on 2016/11/12.
//  Copyright © 2016年 王嘉涛. All rights reserved.
//

import UIKit

class XLMainTabBarController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // 使用自定义tabBar
        let myTabBar = XLTabBar()
        self.setValue(myTabBar, forKey: "tabBar")
        
        myTabBar.buttonClosure = { [weak self] in
            print("别点中央")
            let backView = HMWriteToView()
            backView.show(fromVC: self!)
        }
        
        // 调用⬇️面的方法
        creatChildViewController()
    }
    
    // 调用add..方法创建4个控制器
    private func creatChildViewController(){
        
        addChildViewControllers(vc: XLHomeViewController(), tabImage: "tabbar_home", tabTitle: "首页")
        
        addChildViewControllers(vc: XLDiscoverViewController(), tabImage: "tabbar_discover", tabTitle: "发现")
        
        addChildViewControllers(vc: XLMessageViewController(), tabImage: "tabbar_message_center", tabTitle: "信息")
        
        addChildViewControllers(vc: XLMineViewController(), tabImage: "tabbar_profile", tabTitle: "我的")
    }

    // 封装创建每一个tabBar的子控制器的方法
    private func addChildViewControllers(vc : UIViewController, tabImage : String, tabTitle : String) {
        // 设置tabBar图片文字颜色
        self.tabBar.tintColor = UIColor.orange
        // 设置tabBarcontroller属性
        vc.tabBarItem.image = UIImage(named: tabImage)
        vc.tabBarItem.selectedImage = UIImage(named: tabImage + "_selected")?.withRenderingMode(UIImageRenderingMode.alwaysOriginal)
        vc.tabBarItem.title = tabTitle
        vc.navigationItem.title = tabTitle
        // 设置文字颜色和大小
        vc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName : UIColor.orange], for:.selected)
        vc.tabBarItem.setTitleTextAttributes([NSFontAttributeName : UIFont.systemFont(ofSize: 10)], for: .normal)
        // 设置bage
        vc.tabBarItem.badgeValue = "100"
        vc.tabBarItem.badgeValue = nil
        vc.tabBarItem.badgeColor = #colorLiteral(red: 0.2588235438, green: 0.7568627596, blue: 0.9686274529, alpha: 1)

        let nav = XLBaseViewController(rootViewController: vc)
        self.addChildViewController(nav)

    }
}
